package com.intelligence.tio.server.handler;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.OnlineUserListReqBody;
import com.intelligence.tio.common.packages.OnlineUserListRespBody;
import com.intelligence.tio.common.packages.P2PReqBody;
import com.intelligence.tio.common.packages.P2PRespBody;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.ObjWithLock;
import org.tio.json.Json;

/**
 * 
* @Title: P2PReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 在线用户列表请求消息处理器
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:47:25 
* @version V1.0
 */
public class OnlineUserListReqHandler extends AbsTioBsHandler<OnlineUserListReqBody>
{
	private static Logger log = LoggerFactory.getLogger(OnlineUserListReqHandler.class);

	@Override
	public Class<OnlineUserListReqBody> bodyClass()
	{
		return OnlineUserListReqBody.class;
	}

	@Override
	public Object handler(JsonPacket packet, OnlineUserListReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到在线用户列表请求消息:{}", Json.toJson(bsBody));
		
		//获取请求用户ID
		String userId = channelContext.getSessionContext().getUserId();
		
		List<Map<String, Object>> list = new ArrayList<>();
		
		//获取在线用户列表对象
		ObjWithLock<Set<ChannelContext<TioSessionContext, JsonPacket, Object>>> channelContextsByGroup = Aio.getChannelContextsByGroup(channelContext.getGroupContext(), bsBody.getGroupId());
		if (channelContextsByGroup == null) {
			sendMsg(channelContext, userId, list);
			return null;
		}
		
		//获取用户set集合
		Set<ChannelContext<TioSessionContext, JsonPacket, Object>> obj = channelContextsByGroup.getObj();
		if (obj == null) {
			sendMsg(channelContext, userId, list);
			return null;
		}
		
		Iterator<ChannelContext<TioSessionContext, JsonPacket, Object>> iterator = obj.iterator(); 
		//遍历Set集合获取所有在线用户
		while (iterator.hasNext()) {  
			ChannelContext<TioSessionContext, JsonPacket, Object> temp = iterator.next();
			TioSessionContext sessionContext = temp.getSessionContext();
			Map<String, Object> map = new HashMap<>();
			map.put("userId", sessionContext.getUserId());
			map.put("userName", sessionContext.getUserName());
			map.put("userPhone", sessionContext.getUserPhone());
			map.put("roleId", sessionContext.getRoleId());
			list.add(map);
		}  
		
		sendMsg(channelContext, userId, list);
		
		return null;
	}

	private void sendMsg(ChannelContext<TioSessionContext, JsonPacket, Object> channelContext, String userId,
			List<Map<String, Object>> list) throws UnsupportedEncodingException {
		OnlineUserListRespBody response = new OnlineUserListRespBody();
		response.setList(list);
		response.setCommand(Command.COMMAND_ONLINEUSERLIST_RESP.getValue());
		
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(response).getBytes(JsonPacket.CHARSET));
		Aio.sendToUser(channelContext.getGroupContext(),userId, respPacket);
	}
}
